using System;
using System.Collections.Generic;
using System.Text;

namespace dfp_1._1.clases
{
    public class clsAnalex
    {
        public clsAnalex()
        {
            ;
        }

        public static bool analizar(DiagramaShapes.c_BaseFigure e)
        {
            bool bCorrect = true;
            clsAnalex analizador = new clsAnalex();
            if (e.getFigureName() == "Input")
            {
                bCorrect = analizador.analexEntrada(e.getText());
            }
            else if (e.getFigureName() == "For")
            {
                bCorrect = analizador.analexFor(e.getText());
            }
            else if (e.getFigureName() == "If" || e.getFigureName() == "While")
            {
                bCorrect = analizador.analexIfWhile(e.getText());
            }
            else if (e.getFigureName() == "Process")
            {
                bCorrect = analizador.analexProceso(e.getText());
            }
            return bCorrect;
        }

        private bool analexEntrada(string sString)
        {
            bool bCorrect = true;

            return bCorrect;
        }

        private bool analexFor(string sCadena)
        {
            bool bResp = true;

            return bResp;
        }

        private bool analexIfWhile(string sCadena)
        {
            bool bCorrect = true;
            string sFirstOperand = "";
            string sSecondOperand = "";
            string sOperator = "";

            for (int i = 0; i < sCadena.Length; i++)
            {
                if (isOperator(sCadena[i]))
                    sOperator += sCadena[i];



                else if (isLetter(sCadena[i]) || isOperator(sCadena[i])
                    || isNumber(sCadena[i]))
                {

                    if (isEmpty(sOperator))
                        sFirstOperand += sCadena[i];
                    else
                        sSecondOperand += sCadena[i];
                }


                else if (isOperator(sCadena[i]))
                {
                    if (isEmpty(sOperator))
                        continue;

                    else
                        return false;
                }
            }//del for
            if (!isEmpty(sFirstOperand) && !isEmpty(sSecondOperand))
                if (isBooleanOperator(sOperator))
                    return bCorrect;
                else if (isOperator(sOperator))
                    return bCorrect;
                else
                    return false;
            else
                return false;
        }

        private bool isNumber(Char cCharacter)
        {
            if (cCharacter >= '0' && cCharacter <= '9')
                return true;
            else
                return false;

        }

        private bool isOperator(Char cCharacter)
        {

            if (cCharacter == '_' || cCharacter == '<' ||
                cCharacter == '>' || cCharacter == '!' ||
                cCharacter == '=' || cCharacter == '&' ||
                cCharacter == '|')
                return true;
            else
                return false;

        }

        private bool isEmpty(String sString)
        {

            if (sString == "")
                return true;
            else
                return false;

        }


        private bool isBooleanOperator(String sString)
        {

            if (sString == "==" || sString == "<=" ||
                sString == ">=" || sString == "!=")
                return true;
            else
                return false;
        }


        private bool isLetter(Char cCharacter)
        {
            if (cCharacter >= 'A' && cCharacter <= 'Z' ||
                cCharacter >= 'a' && cCharacter <= 'z')
                return true;
            else
                return false;
        }




        private bool analexProceso(string sCadena)
        {
            bool bResp = true;

            return bResp;
        }

        private bool isOperator(string sCharacter)
        {

            if (sCharacter.Equals('_') || sCharacter.Equals('<') ||
                sCharacter.Equals('>') || sCharacter.Equals('!') ||
                sCharacter.Equals('=') || sCharacter.Equals('&') ||
                sCharacter.Equals('|'))
                return true;
            else
                return false;

        }
    }
}
